package me.shurufa.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import me.shurufa.R;
import me.shurufa.holder.BookCommentViewHolder;
import me.shurufa.model.Comment;
import me.shurufa.widget.CircleImageView;

/**
 * 在图书信息中点击查看全部评价弹出的列表的适配器
 *
 * @FileName: me.shurufa.adapter.BookCommentAdapter.java
 * @author: kHRYSTAL
 * @email: 723526676@qq.com
 * @date: 2016-02-02 15:31
 */
public class BookCommentAdapter extends RecyclerView.Adapter<BookCommentViewHolder> {

    List<Comment> dataList;
    Object parent;


    public BookCommentAdapter(Object parent, List<Comment> dataList) {
        this.dataList = dataList;
        this.parent = parent;
    }

    @Override
    public BookCommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_book_comment_all, parent, false);
        BookCommentViewHolder holder = new BookCommentViewHolder(v, parent);
        return holder;
    }

    @Override
    public void onBindViewHolder(BookCommentViewHolder holder, int position) {
        Comment comment = dataList.get(position);
        holder.bind(comment);
    }

    public void append(Comment[] items) {
        int pos = dataList.size();
        for (Comment item : items) {
            if (!dataList.contains(item))
                dataList.add(item);
        }
        notifyItemRangeInserted(pos, items.length);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void remove(int position) {
        if (dataList.size() > 0) {
            dataList.remove(position);
            this.notifyItemRemoved(position);
        }
    }

    public void clear() {
        int size = dataList.size();
        dataList.clear();
        this.notifyItemRangeRemoved(0, size);
    }
}